{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Numpy 学习笔记\n",
    "\n",
    "## 介绍\n",
    "\n",
    "Numpy 是一个数学函数库，提供了强大的多维数组和用于处理这些数组的函数。它支持高效的数学运算，包括线性代数、傅里叶变换和随机数生成等操作。\n",
    "\n",
    "官方文档：https://numpy.org/doc/stable/user/index.html\n",
    "\n",
    "## 安装\n",
    "\n",
    "```bash\n",
    "pip install numpy\n",
    "```\n",
    "\n",
    "## 基本用法\n",
    "\n",
    "导入 Numpy 库："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c39b15ac68e07886"
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "data": {
      "text/plain": "'1.26.0'"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.__version__"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-09T01:37:19.386888Z",
     "start_time": "2023-12-09T01:37:19.267208Z"
    }
   },
   "id": "6f4305eecbb7412a"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 普通数组\n",
    "\n",
    "Numpy 提供 array() 函数来创建普通数组。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3663902719343cd0"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一维数组：\n",
      "[1 2 3 4 5 6]\n",
      "二维数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "## 一维数组\n",
    "array_1 = np.array([1, 2, 3, 4, 5, 6])\n",
    "\n",
    "## 二维数组\n",
    "array_2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "\n",
    "print(f'一维数组：\\n{array_1}')\n",
    "print(f'二维数组：\\n{array_2}')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-09T01:40:15.187542Z",
     "start_time": "2023-12-09T01:40:15.172582400Z"
    }
   },
   "id": "49ea52862f2431cb"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 序列数组\n",
    "\n",
    "Numpy 提供 `arange()` 函数创建序列数组。序列数组通常是一维数组，它是包含了顺序排列的元素的数组。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "21967c63d2bf6aef"
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[ 5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]\n",
      "[10 12 14 16 18]\n"
     ]
    }
   ],
   "source": [
    "sequence_array_1 = np.arange(10)  # 从 0 开始，以步长为 1 生成不超过 10 的整数序列\n",
    "sequence_array_2 = np.arange(5, 20)  # 从 5 开始，以步长为 1 生成不超过 20 的整数序列\n",
    "sequence_array_3 = np.arange(10, 20, 2)  # 从 10 开始，以步长为 2 生成不超过 20 的整数序列\n",
    "\n",
    "print(sequence_array_1)\n",
    "print(sequence_array_2)\n",
    "print(sequence_array_3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-09T01:50:50.374166700Z",
     "start_time": "2023-12-09T01:50:50.358209Z"
    }
   },
   "id": "96a0d69a27d3cff3"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 随机数组\n",
    "\n",
    "Numpy 提供 `random.rand()` 函数来创建随机小数数组。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e3529da180abe25c"
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "0.8276851238644531"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_array = np.random.rand()\n",
    "\n",
    "random_array"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-09T01:53:47.269372700Z",
     "start_time": "2023-12-09T01:53:47.235464400Z"
    }
   },
   "id": "7c56e9a1a92e23d1"
  },
  {
   "cell_type": "markdown",
   "source": [
    "如果你要创建随机整数数组，使用 `random.randint()`。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1b8db3fb94762066"
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "9"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_array = np.random.randint(10)\n",
    "\n",
    "random_array"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-09T01:56:43.111973500Z",
     "start_time": "2023-12-09T01:56:43.096016600Z"
    }
   },
   "id": "23db69e9d98d4db0"
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-09T01:56:31.950571900Z",
     "start_time": "2023-12-09T01:56:31.936611100Z"
    }
   },
   "id": "4116c7135a6244a7"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "8caa15822c19e75c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
